package com.googlecode.classgrep.visitor.annotated;

import java.util.ArrayList;
import java.util.List;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.EmptyVisitor;

import com.googlecode.classgrep.GrepRobotInternal;
import com.googlecode.classgrep.grep.annotated.MethodGrep;
import com.googlecode.classgrep.info.AnnotationInfo;
import com.googlecode.classgrep.info.MethodInfo;

public class MethodDefVisitor extends CommonDef implements MethodVisitor {

	private MethodInfo info;

	private MethodGrep methodGrep;
	
	public MethodDefVisitor(GrepRobotInternal grepClient, MethodInfo info) {
		super(grepClient);
		this.info = info;
		this.methodGrep = grepClient.getMethodGrep();
	}

	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		AnnotationInfo annotationInfo = new AnnotationInfo(desc);
		if(grepClient.getAnnotationCriterion().contains(annotationInfo.getName())){
	        info.getAnnotations().add(annotationInfo);
	        List<MethodInfo> infos = methodGrep.getAnnotatedMethodInfoMap().get(annotationInfo.getName());
	        if (infos == null) {
	            infos = new ArrayList<MethodInfo>();
	            methodGrep.getAnnotatedMethodInfoMap().put(annotationInfo.getName(), infos);
	        }
	        infos.add(info);
	        return new AnnotationDefVisitor(grepClient, annotationInfo);
		}else{
			return new EmptyVisitor();
		}
	}

	public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
        List<AnnotationInfo> annotationInfos = info.getParameterAnnotations(parameter);
        AnnotationInfo annotationInfo = new AnnotationInfo(desc);
        annotationInfos.add(annotationInfo);
        return new AnnotationDefVisitor(grepClient, annotationInfo);
	}

	
	public AnnotationVisitor visitAnnotationDefault() {
		return null;
	}

	public void visitAttribute(Attribute attr) {

	}

	public void visitCode() {

	}

	public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {

	}

	public void visitInsn(int opcode) {

	}

	public void visitIntInsn(int opcode, int operand) {

	}

	public void visitVarInsn(int opcode, int var) {

	}

	public void visitTypeInsn(int opcode, String type) {

	}

	public void visitFieldInsn(int opcode, String owner, String name, String desc) {

	}

	public void visitMethodInsn(int opcode, String owner, String name, String desc) {

	}

	public void visitJumpInsn(int opcode, Label label) {

	}

	public void visitLabel(Label label) {

	}

	public void visitLdcInsn(Object cst) {

	}

	public void visitIincInsn(int var, int increment) {

	}

	public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {

	}

	public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {

	}

	public void visitMultiANewArrayInsn(String desc, int dims) {

	}

	public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {

	}

	public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {

	}

	public void visitLineNumber(int line, Label start) {

	}

	public void visitMaxs(int maxStack, int maxLocals) {

	}

	public void visitEnd() {

	}

}
